﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Utils.Text;
using QLCONGNOLIBS.Securities;
using QLCONGNOLIBS.Securities.DungDD_SecurityFramework;
using QLCONGNOLIBS.Ultils;

namespace QLCONGNOLIBS.Models.DungDD_Models
{
    class UserModelNew
    {

        //1. Datafield
        private SqlConnection objConnection_g;
        private SqlCommand objSqlCommand_g;
        private SqlDataAdapter objSqlApAdapter_g;
        private DataTable objDataTable_g;
        private tblUser_SC objUserEntity_g;

        //2. Khai báo các thủ tục sẽ sử dụng
        private const string CHECK_LOGIN = "SP_CHECKLOGIN";
        private const string VALIDATE_EMAIL_USERNAME = "RE_SP_ValidateUserNamePassword";
        private const string CHANGE_PASSWORD_USER = "RE_SP_ChangePasswordUser";

        //3. Hàm khởi dựng - Constructors
        public UserModelNew(SqlConnection objConnection, tblUser_SC objUserEntity)
        {
            //Thiết lập thông tin kết nối cho datafield
            objConnection_g = objConnection;
            objUserEntity_g = objUserEntity;
        }

        public UserModelNew(string connectString)
        {
            objConnection_g = new SqlConnection();
            if (objConnection_g.State == ConnectionState.Closed)
            {
                objConnection_g = ConnectionMans.OpenConnection(connectString);
            }
        }
        /// <summary>
        /// Kiểm tra login
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public DataTable CheckElement(string userName, string passWord)
        {
            DataTable objTable;
            try
            {
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@userName", userName);
                param[1] = (new BUltils()).CreateParammeter("@passWord", SecurityConexts.EncryptInfo(passWord));

                //Thực thi truy vấn thông qua Store Procedure
                objTable = new DataTable();
                BUltils obj = new BUltils(objConnection_g);
                objTable = obj.ExecProcReturnTable(CHECK_LOGIN, param);

                //True
                if (objTable.Rows.Count > 0)
                {
                    DataRow row = objTable.Rows[0];
                    objUserEntity_g.IsLoginSuccess = true;
                    objUserEntity_g.Username = userName;
                    objUserEntity_g.Fullname = "" + row["Fullname"];
                    objUserEntity_g.Email = "" + row["Email"];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return objTable;
        }
        /// <summary>
        /// kiểm tra Username và email có khớp nhau hay không
        /// </summary>
        /// <param name="username"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool Validate_Username_Email_m(string username, string email)
        {
            try
            {
                DataTable objTable;
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@userName", username);
                param[1] = (new BUltils()).CreateParammeter("@Email", email);

                //Thực thi truy vấn thông qua Store Procedure
                objTable = new DataTable();
                BUltils obj = new BUltils(objConnection_g);
                objTable = obj.ExecProcReturnTable(VALIDATE_EMAIL_USERNAME, param);

                //true
                if (objTable.Rows.Count > 0)
                {
                    return true;
                }
            }
            catch (Exception)
            {

                throw;
                return false;
            }

            return false;
        }

        public bool ChangePasswordUser_m(string userName, string password)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[2];
                param[0] = (new BUltils()).CreateParammeter("@Username", userName);
                param[1] = (new BUltils()).CreateParammeter("@Password", SecurityConexts.EncryptInfo(password));
                BUltils obj = new BUltils(objConnection_g);
                obj.ExecProcNoReturn(CHANGE_PASSWORD_USER, param);
            }
            catch (Exception)
            {

                throw;
                return false;
            }
            return true;
        }

    }
}
